package com.tanhua.controller;

import com.itheima.commons.utils.JwtUtils;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.UserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/loginReginfo")
    public ResponseEntity<Void> loginReginfo(@RequestBody UserInfo userInfo,
                                             @RequestHeader("Authorization") String token) {
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if (!verifyToken) {
//            throw new RuntimeException("验证码错误");
//        }
//        Claims claims = JwtUtils.getClaims(token);
//        Long id = Long.parseLong(claims.get("id").toString());
        Long id = UserHolder.getUserId();
        userInfo.setId(id);
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginReginfo/head")
    public ResponseEntity<Void> head(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
//        Claims claims = JwtUtils.getClaims(token);
//        Long id =  Long.parseLong(claims.get("id").toString());
        Long id = UserHolder.getUserId();

        userInfoService.updateHead(headPhoto, id);
        return ResponseEntity.ok(null);

    }
}
